package com.reserv.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import com.reserv.db.ConnectionProvider;
import com.reserv.db.JDBCManager;
import com.reserv.vo.CommentVO;

public class CommentDAO implements DAO<CommentVO, Integer> {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	private static CommentDAO instance = new CommentDAO();
	public static CommentDAO getInstance() {
		return instance != null ? instance : new CommentDAO();
	}
	/*private int fno, grade;
	private String uid, comment;
	private Timestamp regDate;*/
	
	@Override
	public void createVO(CommentVO comment) {
		String sql = "INSERT INTO comment(uid, fno, comment, grade) VALUES(?, ?, ?, ?)";
		
		try {
			conn = ConnectionProvider.getConnection();
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, comment.getUid());
			pstmt.setInt(2, comment.getFno());
			pstmt.setString(3, comment.getComment());
			pstmt.setInt(4, comment.getGrade());
			
			pstmt.executeUpdate();
		} catch (NamingException | SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCManager.closeAll(conn, pstmt, rs);
		}
	}

	@Override
	public List<CommentVO> getListVOs() {
		return null;
	}
	public List<CommentVO> getListVOs(int fno) {
		String sql = "SELECT * FROM comment WHERE fno=? ORDER BY regDate DESC";
		List<CommentVO> list = null;
		try {
			conn = ConnectionProvider.getConnection();
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setInt(1, fno);
			
			rs = pstmt.executeQuery();
			if(rs.next()) {
				list = new ArrayList<CommentVO>();
				do {
					CommentVO comment = new CommentVO();
					
					comment.setFno(rs.getInt(1));
					comment.setUid(rs.getString(2));
					comment.setGrade(rs.getInt(3));
					comment.setComment(rs.getString(4));
					comment.setRegDate(rs.getTimestamp(5));
					
					list.add(comment);
				} while(rs.next());
			}
		} catch (NamingException | SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public void updateVO(CommentVO comment) {
		
	}
	@Override
	public CommentVO getVO(Integer p) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteVO(Integer p) {
		// TODO Auto-generated method stub
		
	}
}
